class MME
  #limits for an MME: subscriber capacity, BTS capacity
  #we pretend that BTS capacity will never be exceeded
  #each MME can hold up to 100 users
  MME_user_capacity = 100
  #pool number, weight quotient, MME ID, users number
  attr_accessor :pools, :weight, :id, :users

  def initialize(weight,id)
    @pools = Array.new()
    @weight, @id = weight, id
    @users = 0
  end

  def overload_by_users
    @users - MME_user_capacity
  end

  def handover(source_pool_index,target_pool_index,target_mme_index)
    if self.users>MME_user_capacity
      delta=[self.users-MME_user_capacity,MME_user_capacity-$pools[target_pool_index].mmes[target_mme_index].users].min
    else
      delta=[(self.users/self.pools.size.to_f).floor,[MME_user_capacity-$pools[target_pool_index].mmes[target_mme_index].users,0].max].min
    end
    
    $pools[target_pool_index].mmes[target_mme_index].users+=delta
    $pools[source_pool_index].mme_users_overload-=delta
    self.users-=delta

    p "Handed over #{delta} users from pool #{$pools[source_pool_index].id} MME #{id} to pool #{$pools[target_pool_index].id} MME #{$pools[target_pool_index].mmes[target_mme_index].id}"

    #if MME not in target pool
    unless $pools[target_pool_index].mmes[target_mme_index].pools.include?($pools[source_pool_index].id)
        #add the target MME to the source pool
        $pools[source_pool_index].add_mme($pools[target_pool_index].mmes[target_mme_index])
        p "MME #{$pools[target_pool_index].mmes[target_mme_index].id} is now included in pool #{$pools[source_pool_index].id}"
    end
  end

end